package com.jerry.template.module.mainpage.msg.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.jerry.template.R;
import com.jerry.template.base.BaseActivity;
import com.jerry.template.module.mainpage.msg.db.DBAccess;
import com.jerry.template.module.mainpage.msg.entity.User;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by chenhuan on 2016/5/12.
 */
public class LoginSuccessActivity extends BaseActivity {
    private boolean isqq = false;

    private String sex = "男";

    @BindView(R.id.sex)
    RadioGroup radioGroup;

    @BindView(R.id.user_name)
    EditText name;

    @BindView(R.id.password)
    EditText password;
    private DBAccess dbAccess;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_success_login;
    }

    @OnClick(R.id.login_success_back)
    public void goBack() {
        finish();
    }

    @Override
    protected void initViews() {
        dbAccess = new DBAccess(this);
        Intent intent = getIntent();
        isqq = intent.getBooleanExtra("isQq",false);
        if (isqq) {
            String userName = intent.getStringExtra("userName");
            String userIcon = intent.getStringExtra("userIcon");
            String userPassword = password.getText().toString();
            name.setText(userName);
            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    switch (checkedId) {
                        case R.id.man:
                            sex = "男";
                            break;
                        case R.id.woman:
                            sex = "女";
                            break;
                    }
                }
            });
            loadIcon(userIcon);
            User user = new User(userName,userPassword,sex);
            dbAccess.add(user);
            finish();
        }
    }

    @OnClick(R.id.user_submit)
    public void userSubmit(){
        isLocal();
    }

    private void isLocal() {
        String userName = name.getText().toString();
        String userPassword = password.getText().toString();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.man:
                        sex = "男";
                        break;
                    case R.id.woman:
                        sex = "女";
                        break;
                }
            }
        });
        User user = new User(userName,userPassword,sex);
        dbAccess.add(user);
        finish();
    }

    public void loadIcon(final String imageUrl) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL picUrl = new URL(imageUrl);
                    final Bitmap userIcon = BitmapFactory.decodeStream(picUrl.openStream());
                    FileOutputStream out = null;
                    try {
                        out = new FileOutputStream("/mnt/sdcard/icon.png");
                        userIcon.compress(Bitmap.CompressFormat.PNG, 90, out);// 把数据写入文件
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            out.flush();
                            out.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    LoginSuccessActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ((ImageView) findViewById(R.id.user_icon)).setImageBitmap(userIcon);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
